﻿using System;
using System.Collections.Generic;
using System.Text;
using Emerson.Storage.Agent.Core;
using Emerson.Storage.Agent.DeviceAccess;
using System.Configuration;
using Simulator;

namespace SimulateBattery
{
    public class SimulateBatteryAccess : IBatteryAccess
    {
        BatteryInfo batteryInfo = new BatteryInfo();
        SimulatorService simService;

        public SimulateBatteryAccess()
        {
            int simulatorPort = int.Parse(ConfigurationManager.AppSettings["SimulatorPort"]);
            simService = (SimulatorService)Activator.GetObject(typeof(SimulatorService), "tcp://localhost:" + simulatorPort + "/SimulatorService");
        }

        //SimulatorAccess will connect to the Simulator app on the local machine using .Net remoting.
        public BatteryInfo GetBatteryInfo()
        {
            batteryInfo.IsConnected = false;
            batteryInfo.BatteryType = "Simulator";

            try
            {
                batteryInfo = simService.GetBatteryInfo();
                batteryInfo.LastConnectedTime = DateTime.Now;
                batteryInfo.IsConnected = true;
            }
            catch (Exception ex)
            {
                //Battery is lost!. but we ganna return a BatteryInfo at the last connected time.
            }

            return batteryInfo;
        }
    }
}
